﻿using System.Linq;
using System.Security.Claims;
using K9Nano.LayIM.Utils;
using MediatR;

namespace K9Nano.LayIM.Events
{
    public class ConnectionNotification : INotification
    {
        public string App { get; }

        public string UserId { get; }

        public string[] Roles { get; }

        public string ConnectionId { get; set; }

        public ConnectionNotification(string app, string userId, string[] roles, string connectionId)
        {
            Guard.NotEmpty(connectionId, nameof(connectionId));
            Guard.NotEmpty(app, nameof(app));
            Guard.NotEmpty(userId, nameof(userId));

            App = app;
            UserId = userId;
            Roles = roles ?? new string[0];
            ConnectionId = connectionId;
        }

        public ConnectionNotification(string connectionId, ClaimsPrincipal user, LayIMOptions options)
            : this(
                app: user.FindFirstValue(options.ChatAppCodeClaimType),
                userId: user.FindFirstValue(options.UserIdentifierClaimType),
                roles: user.FindAll(options.RoleClaimType).Select(x => x.Value).ToArray(),
                connectionId: connectionId
                )
        {
        }
    }
}